#include "qwtdiagram.h"
#include "histogram_item.h"
#include <QPen>
#include <QColor>
#include "qwt/include/qwt_plot.h"
#include "qwt/include/qwt_plot_item.h"
#include "qwt/include/qwt_interval_data.h"


QwtDiagram::QwtDiagram(QWidget *parent)
    : QWidget(parent)
{
    plotAffect = new QwtPlot;
    plotAffect->setTitle(tr("Пострадавших..."));
    //plotAffect.setCanvasBackground(QColor(Qt::white));

    plotDead = new QwtPlot;
    plotDead->setTitle(tr("Погибших..."));
    plotDead->setCanvasBackground(QColor(Qt::white));

    plotGrid = new QwtPlotGrid;
    plotGrid->enableXMin(true);
    plotGrid->enableYMin(true);
    plotGrid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
    plotGrid->setMinPen(QPen(Qt::black, 0, Qt::DotLine));
    plotGrid->attach(plotAffect);
    plotGrid->attach(plotDead);

    HistogramItem *histogramAffect = new HistogramItem();
    histogramAffect->setColor(Qt::darkCyan);

    HistogramItem *histogramDead = new HistogramItem();
    histogramDead->setColor(Qt::darkCyan);

    const int numValues = 12;

    //Input data into plotAffect and plotDead
    QwtArray<QwtDoubleInterval> intervals(numValues);
    QwtArray<double> valuesAffect(numValues);
    QwtArray<double> valuesDead(numValues);

    int pos = 0;
    for ( int i = 0; i < (int)intervals.size(); i++ )
    {
        const int width = 10;
        const int value = rand() % 100 - 1;

        intervals[i] = QwtDoubleInterval(pos, pos + width);
        valuesAffect[i] = value;
        valuesDead[i] = value + 1;
        pos += width;
    }

    histogramAffect->setData(QwtIntervalData(intervals, valuesAffect));
    histogramAffect->attach(plotAffect);

    histogramDead->setData(QwtIntervalData(intervals,valuesDead));
    histogramDead->attach(plotDead);

    plotAffect->setAxisScale(QwtPlot::yLeft, 0.0, 100.0);
    plotAffect->setAxisScale(QwtPlot::xBottom, 0.0, pos);
    plotAffect->replot();

    plotDead->setAxisScale(QwtPlot::yLeft, 0.0, 100.0);
    plotDead->setAxisScale(QwtPlot::xBottom, 0.0, pos);
    plotDead->replot();

    plotAffect->setVisible(true);
    plotDead->setVisible(true);

}
